Imports System
Imports System.Web
Imports System.Web.UI.Design
Imports System.ComponentModel
Imports System.IO
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Net.Configuration
Imports System.Configuration.ConfigurationManager

Namespace BA.Controls

    Public Class ControlTemplate
        Inherits ControlDesigner

        Public Overloads Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.Initialize(component)
            SetViewFlags(ViewFlags.TemplateEditing, True)
        End Sub

        Public Overloads Overrides ReadOnly Property TemplateGroups() As TemplateGroupCollection
            Get
                Dim TG As TemplateGroupCollection = New TemplateGroupCollection
                Dim control As TemplateContainer = CType(Me.Component, TemplateContainer)
                Dim group As TemplateGroup = New TemplateGroup("Item")
                Dim template As TemplateDefinition = New TemplateDefinition(Me, "Template", control, "SimpleTemplate", False)
                group.AddTemplateDefinition(template)
                TG.Add(group)
                Return TG
            End Get
        End Property
    End Class

    <ParseChildren(True)> _
    <PersistChildren(True), Designer(GetType(BA.Controls.ControlTemplate))> _
    Public Class TemplatedContainer
        Inherits CompositeControl

#Region " Members "

        Private _title As ITemplate = Nothing
        Private _body As ITemplate = Nothing
        Private _Theme As String = "Default"
        Private _AllowExpandCollapse As Boolean = True
        Private _PrintMe As Boolean = False

#End Region

#Region " Properties "

        Public Property AllowExpandCollapse() As Boolean
            Get
                Return _AllowExpandCollapse
            End Get
            Set(ByVal value As Boolean)
                _AllowExpandCollapse = value
            End Set
        End Property

        Public Property PrintMe() As Boolean
            Get
                Return Convert.ToBoolean(ViewState("printme"))
            End Get
            Set(ByVal value As Boolean)
                ViewState.Add("printme", value)
            End Set
        End Property

        Private Shared ReadOnly Property AppPath() As String
            Get
                Dim path As String = HttpContext.Current.Request.ApplicationPath
                If Not path.EndsWith("/") Then
                    path += "/"
                End If
                Return path
            End Get
        End Property

        <PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(GetType(TemplateControl)), TemplateInstance(TemplateInstance.Single)> _
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property [TitleTemplate]() As ITemplate
            Get
                Return _title
            End Get
            Set(ByVal value As ITemplate)
                _title = value
            End Set
        End Property

        <PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(GetType(TemplateControl)), TemplateInstance(TemplateInstance.Single)> _
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property [BodyTemplate]() As ITemplate
            Get
                Return _body
            End Get
            Set(ByVal value As ITemplate)
                _body = value
            End Set
        End Property

        Public ReadOnly Property TemplateFilePath() As String
            Get

                Dim FilePath As String = String.Empty
                FilePath = HttpContext.Current.Request.MapPath(String.Format("{0}Container/{1}/", AppPath, Theme))

                If File.Exists(String.Format("{0}ContainerTemplate.ascx", FilePath)) Then
                    Return String.Format("{0}Container/{1}/ContainerTemplate.ascx", AppPath, Theme)
                Else
                    Return String.Format("{0}Container/ContainerTemplate.ascx", AppPath)
                End If

            End Get
        End Property

        Public ReadOnly Property Theme() As String
            Get
                If Not String.IsNullOrEmpty(Page.Theme.ToString().Trim()) Then
                    Return Page.Theme
                Else
                    Return _Theme
                End If
            End Get
        End Property

#End Region

#Region " Events "

        Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
            MyBase.OnInit(e)

            Try

                If Not (TemplateFilePath = String.Empty) Then

                    Me.Controls.Add((Page.LoadControl(TemplateFilePath)))

                End If

                If Not (_title Is Nothing) Then

                    Dim PHT As PlaceHolder = Me.Controls.Item(0).FindControl("uxTitle")
                    _title.InstantiateIn(PHT)

                End If

                If Not (_body Is Nothing) Then

                    Dim PHB As PlaceHolder = Me.Controls.Item(0).FindControl("uxBody")
                    _body.InstantiateIn(PHB)

                End If

                Dim EI As Image = Me.Controls.Item(0).FindControl("uxExpandCollapseImage")
                Dim CPE As AjaxControlToolkit.CollapsiblePanelExtender = Me.Controls.Item(0).FindControl("uxContainerCollapsiblePanelExtender")

                If PrintMe Then
                    CPE.Enabled = False
                    EI.Visible = False
                Else
                    CPE.Enabled = _AllowExpandCollapse
                    EI.Visible = _AllowExpandCollapse
                End If

                If _AllowExpandCollapse Then
                    CPE.CollapsedImage = String.Format("~/App_Themes/{0}/images/icons/collapse.gif", Page.Theme.ToString.Trim)
                    CPE.ExpandedImage = String.Format("~/App_Themes/{0}/images/icons/expand.gif", Page.Theme.ToString.Trim)
                End If

            Catch ex As HttpException
                Throw New HttpException("The page template file cannot be found.")
            End Try

        End Sub

#End Region

    End Class

End Namespace